﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类ContractCommisionAdjustInfo。
    /// </summary>
    public class ContractCommisionAdjustInfo
    {
        public ContractCommisionAdjustInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_CommisionAdjustInfo(");
            strSql.Append("RecordId,ContractId,ContractCode,ContractCityId,ContractCity,ContractCompanyId,ContractCompany,AdjustType,AdjustReason,DiscountType,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,Addtime,AddEmployeeId,AddEmployee,AddDepartId,AddDepart,AddCompanyId,AddCompany,AddCity,AddCityId,Status)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ContractId,@ContractCode,@ContractCityId,@ContractCity,@ContractCompanyId,@ContractCompany,@AdjustType,@AdjustReason,@DiscountType,@AdjustDescription,@AdjustMoney,@BeforeAdjust,@AfterAdjust,@Addtime,@AddEmployeeId,@AddEmployee,@AddDepartId,@AddDepart,@AddCompanyId,@AddCompany,@AddCity,@AddCityId,@Status)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "ContractCode", DbType.AnsiString, model.ContractCode);
            db.AddInParameter(dbCommand, "ContractCityId", DbType.Guid, model.ContractCityId);
            db.AddInParameter(dbCommand, "ContractCity", DbType.AnsiString, model.ContractCity);
            db.AddInParameter(dbCommand, "ContractCompanyId", DbType.Guid, model.ContractCompanyId);
            db.AddInParameter(dbCommand, "ContractCompany", DbType.AnsiString, model.ContractCompany);
            db.AddInParameter(dbCommand, "AdjustType", DbType.Int32, model.AdjustType);
            db.AddInParameter(dbCommand, "AdjustReason", DbType.AnsiString, model.AdjustReason);
            db.AddInParameter(dbCommand, "DiscountType", DbType.Int32, model.DiscountType);
            db.AddInParameter(dbCommand, "AdjustDescription", DbType.AnsiString, model.AdjustDescription);
            db.AddInParameter(dbCommand, "AdjustMoney", DbType.Decimal, model.AdjustMoney);
            db.AddInParameter(dbCommand, "BeforeAdjust", DbType.Decimal, model.BeforeAdjust);
            db.AddInParameter(dbCommand, "AfterAdjust", DbType.Decimal, model.AfterAdjust);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddEmployeeId", DbType.Guid, model.AddEmployeeId);
            db.AddInParameter(dbCommand, "AddEmployee", DbType.AnsiString, model.AddEmployee);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddCompanyId", DbType.Guid, model.AddCompanyId);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "AddCity", DbType.AnsiString, model.AddCity);
            db.AddInParameter(dbCommand, "AddCityId", DbType.Guid, model.AddCityId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_CommisionAdjustInfo set ");
            strSql.Append("ContractId=@ContractId,");
            strSql.Append("ContractCode=@ContractCode,");
            strSql.Append("ContractCityId=@ContractCityId,");
            strSql.Append("ContractCity=@ContractCity,");
            strSql.Append("ContractCompanyId=@ContractCompanyId,");
            strSql.Append("ContractCompany=@ContractCompany,");
            strSql.Append("AdjustType=@AdjustType,");
            strSql.Append("AdjustReason=@AdjustReason,");
            strSql.Append("DiscountType=@DiscountType,");
            strSql.Append("AdjustDescription=@AdjustDescription,");
            strSql.Append("AdjustMoney=@AdjustMoney,");
            strSql.Append("BeforeAdjust=@BeforeAdjust,");
            strSql.Append("AfterAdjust=@AfterAdjust,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("AddEmployeeId=@AddEmployeeId,");
            strSql.Append("AddEmployee=@AddEmployee,");
            strSql.Append("AddDepartId=@AddDepartId,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("AddCompanyId=@AddCompanyId,");
            strSql.Append("AddCompany=@AddCompany,");
            strSql.Append("AddCity=@AddCity,");
            strSql.Append("AddCityId=@AddCityId,");
            strSql.Append("Status=@Status");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "ContractCode", DbType.AnsiString, model.ContractCode);
            db.AddInParameter(dbCommand, "ContractCityId", DbType.Guid, model.ContractCityId);
            db.AddInParameter(dbCommand, "ContractCity", DbType.AnsiString, model.ContractCity);
            db.AddInParameter(dbCommand, "ContractCompanyId", DbType.Guid, model.ContractCompanyId);
            db.AddInParameter(dbCommand, "ContractCompany", DbType.AnsiString, model.ContractCompany);
            db.AddInParameter(dbCommand, "AdjustType", DbType.Int32, model.AdjustType);
            db.AddInParameter(dbCommand, "AdjustReason", DbType.AnsiString, model.AdjustReason);
            db.AddInParameter(dbCommand, "DiscountType", DbType.Int32, model.DiscountType);
            db.AddInParameter(dbCommand, "AdjustDescription", DbType.AnsiString, model.AdjustDescription);
            db.AddInParameter(dbCommand, "AdjustMoney", DbType.Decimal, model.AdjustMoney);
            db.AddInParameter(dbCommand, "BeforeAdjust", DbType.Decimal, model.BeforeAdjust);
            db.AddInParameter(dbCommand, "AfterAdjust", DbType.Decimal, model.AfterAdjust);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddEmployeeId", DbType.Guid, model.AddEmployeeId);
            db.AddInParameter(dbCommand, "AddEmployee", DbType.AnsiString, model.AddEmployee);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddCompanyId", DbType.Guid, model.AddCompanyId);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "AddCity", DbType.AnsiString, model.AddCity);
            db.AddInParameter(dbCommand, "AddCityId", DbType.Guid, model.AddCityId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_CommisionAdjustInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,ContractCode,ContractCityId,ContractCity,ContractCompanyId,ContractCompany,AdjustType,AdjustReason,DiscountType,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,Addtime,AddEmployeeId,AddEmployee,AddDepartId,AddDepart,AddCompanyId,AddCompany,AddCity,AddCityId,Status from t_Contract_CommisionAdjustInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,ContractCode,ContractCityId,ContractCity,ContractCompanyId,ContractCompany,AdjustType,AdjustReason,DiscountType,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,Addtime,AddEmployeeId,AddEmployee,AddDepartId,AddDepart,AddCompanyId,AddCompany,AddCity,AddCityId,Status ");
            strSql.Append(" FROM t_Contract_CommisionAdjustInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,ContractCode,ContractCityId,ContractCity,ContractCompanyId,ContractCompany,AdjustType,AdjustReason,DiscountType,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,Addtime,AddEmployeeId,AddEmployee,AddDepartId,AddDepart,AddCompanyId,AddCompany,AddCity,AddCityId,Status ");
            strSql.Append(" FROM t_Contract_CommisionAdjustInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo> list = new List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model = new Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ContractId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractId = new Guid(ojb.ToString());
            }
            model.ContractCode = dataReader["ContractCode"].ToString();
            ojb = dataReader["ContractCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractCityId = new Guid(ojb.ToString());
            }
            model.ContractCity = dataReader["ContractCity"].ToString();
            ojb = dataReader["ContractCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractCompanyId = new Guid(ojb.ToString());
            }
            model.ContractCompany = dataReader["ContractCompany"].ToString();
            ojb = dataReader["AdjustType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AdjustType = (int)ojb;
            }
            model.AdjustReason = dataReader["AdjustReason"].ToString();
            ojb = dataReader["DiscountType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DiscountType = (int)ojb;
            }
            model.AdjustDescription = dataReader["AdjustDescription"].ToString();
            ojb = dataReader["AdjustMoney"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AdjustMoney = (decimal)ojb;
            }
            ojb = dataReader["BeforeAdjust"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BeforeAdjust = (decimal)ojb;
            }
            ojb = dataReader["AfterAdjust"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AfterAdjust = (decimal)ojb;
            }
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            ojb = dataReader["AddEmployeeId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddEmployeeId = new Guid(ojb.ToString());
            }
            model.AddEmployee = dataReader["AddEmployee"].ToString();
            ojb = dataReader["AddDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddDepartId = new Guid(ojb.ToString());
            }
            model.AddDepart = dataReader["AddDepart"].ToString();
            ojb = dataReader["AddCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddCompanyId = new Guid(ojb.ToString());
            }
            model.AddCompany = dataReader["AddCompany"].ToString();
            model.AddCity = dataReader["AddCity"].ToString();
            ojb = dataReader["AddCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddCityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            return model;
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetPartList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        #endregion  成员方法
    }
}

